package com.lxt.cloud.testhystrix.controller;

import com.lxt.cloud.testhystrix.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalTime;

@RestController
public class HystrixController {

        @Autowired
        private HystrixService hystrixService;

        @Value("${server.port}")
        private String serverPost;

        @GetMapping("/hystrix/OK/{id}")
        public String paymentInfo_OK(@PathVariable("id") Long id){
            return hystrixService.paymentInfo_OK(id);
        }

        @GetMapping("/hystrix/Timeout/{id}")
        public String paymentInfo_TimeOut(@PathVariable("id") Long id){
            LocalTime t= LocalTime.now();
            System.out.println(t);
            return hystrixService.paymentInfo_TimeOut(id);
        }
        //==========服务熔断
        //测试方法，先一直传负数参数，导致断路器打开，再传正数，会发现不会马上通过正确的方法，依然会报错一会，才慢慢恢复。
        @GetMapping("/hystrix/circuit/{id}")
        public String paymentCircuitBreaker(@PathVariable("id") Integer id){
            String result = hystrixService.paymentCircuitBreaker(id);

            System.out.println("**********result: " + result);

            return result;
        }
    }

